﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;
using Enterprise.Service.Jihuabu;
using Enterprise.Model.Jihuabu;

namespace Enterprise.UI.Web.Modules.Jihuabu
{
    public partial class M01_JDB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindRQ();
                bindGrid();
            }
        }

        //绑定已录入的时间
        private void bindRQ()
        {
            d_rq.Items.Clear();
            M01JDBService service = new M01JDBService();
            d_rq.DataSource = service.getRiqiList("");
            d_rq.DataTextField = "WCSJ";
            d_rq.DataValueField = "WCSJ";
            d_rq.DataBind();
        }

        //绑定表格
        private void bindGrid()
        {
            M01JDBService service = new M01JDBService();
            gv.DataSource = service.GetListbySQL("select a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,a.rowid from jhb_m01_jdb a ,jhb_m01_xmb b where a.wcsj='"+d_rq.SelectedValue+"' and a.afebh=b.afebh(+)");
            gv.DataBind();
        }

        //复制上次录入的数据
        protected void btn_create_fromlast_Click(object sender, EventArgs e)
        {
            if (t_rq.Text == "")
            {
                Utility.ShowMsg(this.Page, "请输入一个日期！");
            }
            else
            {
                //读取上次录入的项目清单
            }
        }

        //从项目表创建
        protected void btn_create_fromxmb_Click(object sender, EventArgs e)
        {
            if (t_rq.Text == "")
            {
                Utility.ShowMsg(this.Page, "请输入一个日期！");
            }
            else
            {
                //读取所有未完成的项目
                Enterprise.Service.Jihuabu.M01JDBService jdbService = new M01JDBService();
                jdbService.CreateDataFromXmb(t_rq.Text);
                bindRQ();
                bindGrid();
            }
        }

        protected void d_rq_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindGrid();
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Enterprise.Service.Jihuabu.M01JDBService service = new M01JDBService();
            string sqlUpdate = "begin ";
            //判断是否是最新的数据
            bool isnew = service.isNew(d_rq.SelectedValue);
            //保存
            foreach (GridViewRow row in gv.Rows)
            {
                string _rowid = row.Cells[9].Text;
                TextBox tb = (TextBox)row.Cells[8].FindControl("tb_wcjg");
                string _wcjg = (string.IsNullOrEmpty(tb.Text) ? "NULL" : tb.Text);
                sqlUpdate += "update jhb_m01_jdb set wc_jg=" + _wcjg + ",lrr='"+Page.User.Identity.Name+"',lrsj=to_date('" + DateTime.Now + "','YYYY-MM-DD HH24:MI:SS') where rowid='" + _rowid + "';";
                //如果是最新的数据 将实际金戈更新到项目表
                if (isnew&&_wcjg!="NULL")
                {
                    string _afebh = row.Cells[0].Text;                   
                    sqlUpdate += "update jhb_m01_xmb set shiji_jg= "+_wcjg+",gxrq='"+d_rq.SelectedValue+"' where afebh='"+_afebh+"';";
                }
            }
            sqlUpdate += "null;end;";            
            service.ExcuteUpdateSql(sqlUpdate);
            bindGrid();
            
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[9].Visible = false;
        }

        protected void Btn_AddNew_Click(object sender, EventArgs e)
        {
            //新增AFE记录
            
            M01_JDBEntity model = new M01_JDBEntity();
            Enterprise.Service.Jihuabu.M01JDBService service = new M01JDBService();
            model.AFEBH = TextBox1.Text;
            model.WCSJ = d_rq.SelectedValue;
            if (!string.IsNullOrEmpty(model.AFEBH)
                && !string.IsNullOrEmpty(model.WCSJ)
                && !service.Exists(model.AFEBH, model.WCSJ))
            {
                Enterprise.Service.Jihuabu.M01XMBService xms = new M01XMBService();
                if (xms.Exists(model.AFEBH))
                {
                    service.Add(model);
                }
                else
                    Enterprise.Infrastructure.Utility.ShowMsg(this.Page, "项目表中没有发现这个项目");
            }
            else
            {
                Enterprise.Infrastructure.Utility.ShowMsg(this.Page,"请检查AFE编号是否为空或已经存在");
            }
        }

        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Enterprise.Service.Jihuabu.M01JDBService jds = new M01JDBService();
            string _afebh = gv.Rows[e.RowIndex].Cells[0].Text;
            string _wcsj = d_rq.SelectedValue;
            jds.Delete(_afebh, _wcsj);
            bindGrid();
        }
    }
}